/*
 * ${tableModel.domainName}ServiceImpl.java  Created On ${tableModel.currentDate}
 * Copyright(c) 2015 Treebear Inc.
 * ALL Rights Reserved.
 */
package ${packname}.service.impl;

import ${packname}.dao.${tableModel.domainName}Dao;
import ${packname}.entity.${tableModel.domainName};
import ${packname}.service.${tableModel.domainName}Service;
import com.fsnip.common.tools.base.entity.PageList;
import com.fsnip.common.tools.base.exception.ServiceException;
import com.fsnip.common.tools.utils.I18nSupport;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 类${tableModel.domainName}ServiceImpl的实现描述：${tableModel.tableChineseName}
 * @author ${author} ${tableModel.currentDate} ${tableModel.currentTime}
 */
@Service
public class ${tableModel.domainName}ServiceImpl implements ${tableModel.domainName}Service  {
    
	private Log LOG = LogFactory.getLog (${tableModel.domainName}.class);
	
	@Autowired
	I18nSupport i18nSupport;

	@Autowired(required = true)
	private ${tableModel.domainName}Dao  dao;
	
    
    /**
	 * 增加一条记录
	 * 
	 * @param entity 需要添加的实体
	 * @return 增加之后的实体。
	 */
	public ${tableModel.domainName} add(${tableModel.domainName} entity) {
		
		LOG.debug("开始添加${tableModel.tableChineseName}信息");
		
		try {
			Integer id = dao.create(entity);
		} catch (Exception e) {
			LOG.error("添加${tableModel.tableChineseName}信息失败");
			throw new ServiceException(i18nSupport.getMessage("common.delete.record.error"));
		}
		
		LOG.debug("添加成功");
		
		return entity;
	}
	
	/**
	 * 更新实体
	 * 
	 * @param entity 需要更新的实体。注意：仅包含主键及需要更新的字段和值
	 * @return 更新之后的实体。注意：需要包含该实体的完整数据，否则会出现显示错误
	 */
	public ${tableModel.domainName} update(${tableModel.domainName} entity) {
		
		LOG.debug("开始更新${tableModel.tableChineseName}信息");
		
		try {
			int count = dao.update(entity);
		} catch (Exception e) {
			LOG.error("更新${tableModel.tableChineseName}信息失败");
			throw new ServiceException(i18nSupport.getMessage("common.delete.record.error"));
		}	
		
		LOG.debug("更新成功");
		
		return entity;
	}
	
  /**
	 * 删除实体
	 * 
	 * @param entity 需要删除的实体
	 * 
	 */
	public void remove(${tableModel.domainName} entity) {
		
		LOG.debug("删除更新${tableModel.tableChineseName}信息");
		
		try {
	<#list tableModel.columnList as columnModel>
		<#if columnModel.isPK==true>
			dao.deleteByPrimaryKey(entity.get${columnModel.columnName2Pascal}());
  		</#if>
	</#list>
		} catch (Exception e) {
			LOG.error("删除信息失败！");
			throw new ServiceException(i18nSupport.getMessage("common.delete.record.error"));
		}
		
		LOG.debug("删除成功");
	}
	
	/**
   * 根据主键提取信息
   *
   * @param id 主键信息
   * @return 查询到的信息
   */
	<#list tableModel.columnList as columnModel>
	<#if columnModel.isPK == true>
   public ${tableModel.domainName} findByPrimaryKey(${columnModel.columnType} id){
   </#if>
   </#list>
	   return dao.getByPrimaryKey(id);
   }
	
	/**
	 * 根据查询条件查询结果集
	 * 
	 * @param pageList 查询条件（包含 查询字符串、排序、分页以及页大小
	 * @return 查询结果
	 */
	public PageList<${tableModel.domainName}> getByCondition(PageList<${tableModel.domainName}> pageList) {

		if (LOG.isDebugEnabled()) {
			LOG.debug("分页查询结果集，输入条件为：" + pageList);
		}
		List<${tableModel.domainName}> record = dao.getByCondition(pageList);
		pageList.setItems(record);
		if (LOG.isDebugEnabled()) {
			LOG.debug("查詢完成");
		}
		return pageList;
	}
    
}